﻿using System.Collections.Generic;
using System.Data;
using DataAccess.Implementations;
using NH.Domain.Implementations;
using NH.Domain.Interfaces;
using NH.Provider.Interfaces;

namespace NH.Provider.Implementations
{
    public class DepartmentHandler : IDepartmentHandler
    {
         private readonly SqlHelper _sqlHelper;

         public DepartmentHandler()
        {
            _sqlHelper = new SqlHelper();
        }

        public IList<IDepartment> GetDepartments()
        {
            var departments = new List<IDepartment>();

            try
            {
                _sqlHelper.OpenConnection();
                _sqlHelper.CreateCommand("dbo.GetDepartmentsList", CommandType.StoredProcedure);

                var sqlReader = _sqlHelper.ExecuteReader(CommandBehavior.CloseConnection);
                while (sqlReader.Read())
                {
                    departments.Add(new Department
                                    {
                                        Id = SqlUtil.SetValue(sqlReader["ID"], 0),
                                        Name = SqlUtil.SetValue(sqlReader["Name"], string.Empty),
                                    });
                }
            }
            finally
            {
                _sqlHelper.CloseConnection();
            }

            return departments;
        }
    }
}